
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from datetime import datetime


lists = [1,2,3,4,5]
print(np.min(lists))
print(np.max(lists))
print(np.median(lists))
print(np.mean(lists))

class group_data:
    def __init__(self):
        self.id = 0;
        self.time = "";
        self.bid_index  = 0;
        self.max = 0
        self.count = 0

class group_s:
    def __init__(self,b,e,id):
        self.id = id;
        self.time = "";
        self.bid_index  = 0;
        self.max_back = 0
        self.count = 0
        self.begin = b
        self.end = e
        self.back= []
        self.latency= []
        self.total= 0 #某个group 有多少个msg
        self.mean = 0
        self.std = 0
        self.median = 0
        self.min = 0
        self.max = 0
        self.percent = 0
        self.line = 0
        self.begin_line = 0

#g_930_10 = group_s("09:30:00.0","10:00:00.0")
#g_10_1030 = group_s("10:00:00.0","10:30:00.0",)
#g_1030_11 = group_s("10:30:00.0","11:00:00.0")
#g_11_1130 = group_s("11:00:00.0","11:30:00.0")
#g_13_1330 = group_s("13:00:00.0","13:30:00.0")
#g_1330_14 = group_s("13:30:00.0","14:00:00.0")
#g_14_1430 = group_s("14:00:00.0","14:30:00.0")
#g_1430_1456 = group_s("14:30:00.0","14:56:59.9")
#g_a = group_s("09:30:00.0","14:56:59.9")
#
#g_list =[]
#g_list.append(g_930_10)
#g_list.append(g_10_1030)
#g_list.append(g_1030_11)
#g_list.append(g_11_1130)
#g_list.append(g_13_1330)
#g_list.append(g_1330_14)
#g_list.append(g_14_1430)
#g_list.append(g_a)
group_dict ={} 
group_seg ={} #容纳 mean等数值对应的struct

group_a=[]
group_a.append(group_s("09:30:00.0","10:00:00.0",999))
group_a.append(group_s("10:00:00.0","10:30:00.0",999))
group_a.append(group_s("10:30:00.0","11:00:00.0",999))
group_a.append(group_s("11:00:00.0","11:30:00.0",999))
group_a.append(group_s("13:00:00.0","13:30:00.0",999))
group_a.append(group_s("13:30:00.0","14:00:00.0",999))
group_a.append(group_s("14:00:00.0","14:30:00.0",999))
group_a.append(group_s("14:30:00.0","14:56:59.9",999))
group_a.append(group_s("09:30:00.0","14:56:59.9",999))
 
#group_seg_a[999] = mylist

def get():
    for line in open('test', 'r'):
        values =  line.split(',')
        if values[0] =="td SSE":
            time_idx = 2
            group_idx = 5
            bid_idx = 11
        else:
            time_idx = 2
            group_idx = 5
            bid_idx = 9
        curr = group_data()
        curr.id = values[group_idx]
        curr.time = values[time_idx]
        curr.bid_index = values[bid_idx]             
        if (curr.id in group_dict): #group_dict 中存在id
            pre = group_dict[curr.id]
            if (int(curr.bid_index) - int(pre.bid_index) < 0 ): #有乱序发生
                diff = -(int(curr.bid_index) - int(pre.bid_index))
                if (diff > pre.max):
                    pre.max = diff
                stamp_pre =pre.time 
                stamp_curr =curr.time 
                diff_time = (datetime.strptime(stamp_pre, '%H:%M:%S.%f') - datetime.strptime(stamp_curr, '%H:%M:%S.%f')).total_seconds() 
                pre.count = pre.count+1
                #print("group,",curr.id ,",back,",diff,",diff_time(s),",diff_time,",max,",pre.max,",back count,",pre.count,",curr_bid,", curr.bid_index  )
                pre.bid_index = curr.bid_index
                pre.time = curr.time

                templist = group_seg[curr.id]
                for ele in templist:
                    if datetime.strptime(ele.begin, '%H:%M:%S.%f') <= datetime.strptime(values[time_idx], '%H:%M:%S.%f') and  datetime.strptime(ele.end, '%H:%M:%S.%f') > datetime.strptime(values[time_idx], '%H:%M:%S.%f'):
                        ele.latency.append(diff_time)
                        ele.back.append(diff)
                templist2 = group_seg[999]
                for ele in templist2:
                    if datetime.strptime(ele.begin, '%H:%M:%S.%f') <= datetime.strptime(values[time_idx], '%H:%M:%S.%f') and  datetime.strptime(ele.end, '%H:%M:%S.%f') > datetime.strptime(values[time_idx], '%H:%M:%S.%f'):
                        ele.latency.append(diff_time)
                        ele.back.append(diff)

 
            else:  #没有乱序发生
                pre.bid_index = curr.bid_index
                pre.time = curr.time
            templist = group_seg[curr.id]
            for ele in templist:
                    if datetime.strptime(ele.begin, '%H:%M:%S.%f') <= datetime.strptime(values[time_idx], '%H:%M:%S.%f') and   datetime.strptime(values[time_idx], '%H:%M:%S.%f')< datetime.strptime(ele.end, '%H:%M:%S.%f') :
                        ele.total = ele.total+1
            templist2 = group_seg[999]
            for ele in templist2:
                    if datetime.strptime(ele.begin, '%H:%M:%S.%f') <= datetime.strptime(values[time_idx], '%H:%M:%S.%f') and   datetime.strptime(values[time_idx], '%H:%M:%S.%f')< datetime.strptime(ele.end, '%H:%M:%S.%f') :
                        ele.total = ele.total+1

        else:
             g_930_10 = group_s("09:30:00.0","10:00:00.0",curr.id)
             g_10_1030 = group_s("10:00:00.0","10:30:00.0",curr.id)
             g_1030_11 = group_s("10:30:00.0","11:00:00.0",curr.id)
             g_11_1130 = group_s("11:00:00.0","11:30:00.0",curr.id)
             g_13_1330 = group_s("13:00:00.0","13:30:00.0",curr.id)
             g_1330_14 = group_s("13:30:00.0","14:00:00.0",curr.id)
             g_14_1430 = group_s("14:00:00.0","14:30:00.0",curr.id)
             g_1430_1456 = group_s("14:30:00.0","14:56:59.9",curr.id)
             g_a =         group_s("09:30:00.0","14:56:59.9",curr.id)
             
             g_list =[]
             g_list.append(g_930_10)
             g_list.append(g_10_1030)
             g_list.append(g_1030_11)
             g_list.append(g_11_1130)
             g_list.append(g_13_1330)
             g_list.append(g_1330_14)
             g_list.append(g_14_1430)
             g_list.append(g_a)
             group_dict[curr.id] = curr
             group_seg[curr.id] = g_list
             templist = group_seg[999]
             for ele in templist:
                    if datetime.strptime(ele.begin, '%H:%M:%S.%f') <= datetime.strptime(values[time_idx], '%H:%M:%S.%f') and   datetime.strptime(values[time_idx], '%H:%M:%S.%f')< datetime.strptime(ele.end, '%H:%M:%S.%f') :
                        ele.total = ele.total+1



def get2():
    for line in open('time4.log', 'r'):
    #for line in open('test.log', 'r'):
        values =  line.split(',')
        if values[0] =="td SSE":
            time_idx = 17
            group_idx = 1
            bid_idx = 12
            back_idx = 3
            line_idx = 15
        else:
            time_idx = 17
            group_idx = 1
            bid_idx = 12
            back_idx = 3
            line_idx = 15
        curr = group_data()
        values[time_idx] =values[time_idx].replace("\n","")
        curr.id = values[group_idx]
        curr.time = values[time_idx]
        curr.bid_index = values[bid_idx]             
        if (curr.id in group_dict): #group_dict 中存在id
            pre = group_dict[curr.id]
            templist = group_seg[curr.id]
            for ele in templist:
                    if datetime.strptime(ele.begin, '%H:%M:%S.%f') <= datetime.strptime(values[time_idx], '%H:%M:%S.%f') and   datetime.strptime(values[time_idx], '%H:%M:%S.%f')< datetime.strptime(ele.end, '%H:%M:%S.%f') :
                        if ele.begin_line == 0:
                            ele.begin_line = int(values[line_idx])
                        ele.total = int(values[line_idx])
                        ele.back.append(int(values[back_idx]))

            for ele in group_a:
                    if datetime.strptime(ele.begin, '%H:%M:%S.%f') <= datetime.strptime(values[time_idx], '%H:%M:%S.%f') and   datetime.strptime(values[time_idx], '%H:%M:%S.%f')< datetime.strptime(ele.end, '%H:%M:%S.%f') :
                        ele.begin_line = 0
                        #for key in group_seg:
                        #        for ssubele in group_seg[key]:
                        #            if datetime.strptime(ssubele.begin, '%H:%M:%S.%f') <= datetime.strptime(values[time_idx], '%H:%M:%S.%f') and   datetime.strptime(values[time_idx], '%H:%M:%S.%f')< datetime.strptime(ssubele.end, '%H:%M:%S.%f') :
                        #                ele.begin_line = ele.begin_line+int(ssubele.begin_line)

                        for subele in group_seg:
                            ele.total = 0
                            ele.back.append(int(values[back_idx]))
                            for ssubele in group_seg[subele]:
                                ele.total = ele.total +ssubele.total
                             
        else:
             g_930_10 =  group_s("09:30:00.0","10:00:00.0",curr.id)
             g_10_1030 = group_s("10:00:00.0","10:30:00.0",curr.id)
             g_1030_11 = group_s("10:30:00.0","11:00:00.0",curr.id)
             g_11_1130 = group_s("11:00:00.0","11:30:00.0",curr.id)
             g_13_1330 = group_s("13:00:00.0","13:30:00.0",curr.id)
             g_1330_14 = group_s("13:30:00.0","14:00:00.0",curr.id)
             g_14_1430 = group_s("14:00:00.0","14:30:00.0",curr.id)
             g_1430_1456 = group_s("14:30:00.0","14:56:59.9",curr.id)
             g_a =         group_s("09:30:00.0","14:56:59.9",curr.id)
             
             g_list =[]
             g_list.append(g_930_10)
             g_list.append(g_10_1030)
             g_list.append(g_1030_11)
             g_list.append(g_11_1130)
             g_list.append(g_13_1330)
             g_list.append(g_1330_14)
             g_list.append(g_14_1430)
             g_list.append(g_a)
             group_dict[curr.id] = curr
             group_seg[curr.id] = g_list
             for ele in group_a:
                    if datetime.strptime(ele.begin, '%H:%M:%S.%f') <= datetime.strptime(values[time_idx], '%H:%M:%S.%f') and   datetime.strptime(values[time_idx], '%H:%M:%S.%f')< datetime.strptime(ele.end, '%H:%M:%S.%f') :
                        ele.back.append(int(values[back_idx]))
                        for subele in group_seg:
                            ele.total = int(values[line_idx])
                            for ssubele in group_seg[subele]:
                                ele.total = ele.total + ssubele.total




get2()

for key in group_seg:
    currlist = group_seg[key]
    for ele in currlist:
        if (len(ele.back)==0):
            continue
        ele.min = np.min(ele.back)
        ele.max = np.max(ele.back)
        ele.mean = np.mean(ele.back)
        ele.median = np.median(ele.back)
        ele.std = np.std(ele.back)
        ele.percent = float(len(ele.back)/(ele.total-ele.begin_line))
        print("id:",ele.id,"min:",ele.min,"max:",ele.max,"mean:",ele.mean,"median:",ele.median,"std:",ele.std,"percent:",ele.percent,"Time Range:",ele.begin,ele.end)
for ele in group_a:


        if (len(ele.back)==0):
            continue
        ele.min = np.min(ele.back)
        ele.max = np.max(ele.back)
        ele.mean = np.mean(ele.back)
        ele.median = np.median(ele.back)
        ele.std = np.std(ele.back)
        ele.percent = float(len(ele.back)/(ele.total-ele.begin_line))
        print("id:",ele.id,"min:",ele.min,"max:",ele.max,"mean:",ele.mean,"median:",ele.median,"std:",ele.std,"percent:",ele.percent,"Time Range:",ele.begin,ele.end)
        #print(len(ele.back))
        #print(ele.total)

for key in group_seg:
    currlist = group_seg[key]
    for ele in currlist:
        if (len(ele.back)==0):
            continue
        ele.min = np.min(ele.back)
        ele.max = np.max(ele.back)
        ele.mean = np.mean(ele.back)
        ele.median = np.median(ele.back)
        ele.std = np.std(ele.back)
        ele.percent = float(len(ele.back)/(ele.total-ele.begin_line))
        
        strs2= "id,"+str(ele.id)+",min,"+str(ele.min)+",max,"+str(ele.max)+",mean,"+str(ele.mean)+",median,"+str(ele.median)+",std,"+str(ele.std)+",percent,"+str(ele.percent)+",Time Range:"+str(ele.begin)+"--"+str(ele.end)
        print(strs2)

for ele in group_a:

        if (len(ele.back)==0):
            continue
        ele.min = np.min(ele.back)
        ele.max = np.max(ele.back)
        ele.mean = np.mean(ele.back)
        ele.median = np.median(ele.back)
        ele.std = np.std(ele.back)
        ele.percent = float(len(ele.back)/(ele.total-ele.begin_line))
        
        strs2= "id,"+str(ele.id)+",min,"+str(ele.min)+",max,"+str(ele.max)+",mean,"+str(ele.mean)+",median,"+str(ele.median)+",std,"+str(ele.std)+",percent,"+str(ele.percent)+",Time Range:"+str(ele.begin)+"--"+str(ele.end)
        print(strs2)

        #print(len(ele.back))
        #print(ele.total)


print("end")















































